package com.cmfi.controller;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cmfi.domain.Line;
import com.cmfi.service.inter.LineService;
import com.cmfi.utils.Code;
import com.cmfi.utils.Result;

import lombok.extern.slf4j.Slf4j;

//return都是result

@Slf4j
@RestController
@RequestMapping("/api/line")
public class LineController {
    @Autowired
    private LineService lineService;
    static Logger log = LoggerFactory.getLogger(LineController.class);
    
    /**
     * {@code PUT  /line/:id} : Updates an existing line.
     *
     * @param id the id of the line to save.
     * @param line the line to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated line,
     * or with status {@code 400 (Bad Request)} if the line is not valid,
     * or with status {@code 500 (Internal Server Error)} if the line couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/line/{id}")
    @PreAuthorize("hasAuthority('line')")
	public Result updateLine(@PathVariable(value = "id", required = false) final Long id,
			@RequestBody Line line) {
		log.info("[LineController][updateLine] line:{},", line);
		if (id == null) {
			return new Result(Code.UPDATE_ERR, "计划ID为空");
		}
		try {
			lineService.update(line, id);//调用对象！！！！！开头第一个字母小写！！！！！
			return new Result(Code.UPDATE_OK, line);
		} catch (Exception e) {
			log.error("[LineController][updateLine] e,", e);
			return new Result(Code.UPDATE_ERR, e.getMessage());
		}
	}
     
    /**
     * Get the output of lines.
     * @param null.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the Equipment, or with status {@code 404 (Not Found)}.
     */
    @GetMapping( "/todayOutput")
    @PreAuthorize("hasAuthority('line')")
    public Result getTodayOutput() {
    	log.debug("REST request to get the output of each line.");
		//List<Line> lineOutputList = null;//有两条产线
		//long total = 0;
    	long lineOutput = 0;
		try {
			lineOutput = lineService.getTodayOutput();
			//total = lineOutputList.size();
		} catch (Exception e) {
			log.error("[LineController][getTodayOutput] e:{}", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
		return new Result(Code.GET_OK, lineOutput);
    }
    
    /**
     * Get the output of historical output.
     * @param beginningTime, endTime. The time that customers need.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the Equipment, or with status {@code 404 (Not Found)}.
     */
    @GetMapping(value = "/getHistoryOutput", produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize("hasAuthority('line')")
    public Result getHistoryOutput(@RequestParam Map<String, Object> params) {
    	log.debug("REST request to get a page of Equipments' warnings.");
		//List<Line> historyOtputList = null;
    	long historyOtput = 0;
		//long total = 0;
		try {
			if (params == null) {
				return new Result(Code.GET_ERR, "入参为空");
			}
			String beginningTime = null;//日期格式例子：1920-09-01
			String endTime = null;//日期格式例子：1920-09-02
			if (params.containsKey("beginningTime")) {
				beginningTime = params.get("beginningTime").toString();
			}
			if (params.containsKey("endTime")) {
				endTime = params.get("endTime").toString();
			}
			historyOtput = lineService.getHistoryOutput(beginningTime, endTime);
			//total = historyOtputList.size();
		} catch (Exception e) {
			log.error("[LineController][getHistoryOutput] e:{}", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
		return new Result(Code.GET_OK, historyOtput);
    }
    
    /**
     * Get the operating efficiency of lines.
     * @param null.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the Equipment, or with status {@code 404 (Not Found)}.
     */
    @GetMapping( "/getOperatingEfficiency")
    @PreAuthorize("hasAuthority('line')")
    public Result getOperatingEfficiency() {
    	log.debug("REST request to get the output of each line.");
		//List<Line> operatingEfficiencyList = null;
    	long operatingEfficiency = 0;
		//long total = 0;
		try {
			operatingEfficiency = lineService.getOperatingEfficiency();
			//total = operatingEfficiencyList.size();
		} catch (Exception e) {
			log.error("[LineController][getOperatingEfficiency] e:{}", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
		return new Result(Code.GET_OK, operatingEfficiency);
    }
    
    /**
     * Get the today's running time of lines.
     * @param null.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the Equipment, or with status {@code 404 (Not Found)}.
     */
    @GetMapping( "/getTodayRunningTime")
    @PreAuthorize("hasAuthority('line')")
    public Result getTodayRunningTime() {
    	log.debug("REST request to get the output of each line.");
    	List<HashMap<Object, String>> todayRunningTimeList = null;
		long total = 0;
		try {
			todayRunningTimeList = lineService.getTodayRunningTime();
			total = todayRunningTimeList.size();
		} catch (Exception e) {
			log.error("[LineController][getTodayRunningTime] e:{}", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
		return new Result(Code.GET_OK, todayRunningTimeList, total);
    }
    
    /**
     * Get the total running time of lines.
     * @param beginningTime, endTime. The time that customers need.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the Equipment, or with status {@code 404 (Not Found)}.
     */
    @GetMapping(value = "/getTotalRunningTime", produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize("hasAuthority('line')")
    public Result getTotalRunningTime(@RequestParam Map<String, Object> params) {
    	log.debug("REST request to get a page of Equipments' warnings.");
    	List<HashMap<Object, String>> totalRunningTimeList = null;
    	//long totalRunningTime = 0;//运行的总秒数
		long total = 0;
		try {
			if (params == null) {
				return new Result(Code.GET_ERR, "入参为空");
			}
			String beginningTime = null;
			String endTime = null;
			if (params.containsKey("beginningTime")) {
				beginningTime = params.get("beginningTime").toString();
			}
			if (params.containsKey("endTime")) {
				endTime = params.get("endTime").toString();
			}
			totalRunningTimeList = lineService.getTotalRunningTime(beginningTime, endTime);
			total = totalRunningTimeList.size();
		} catch (Exception e) {
			log.error("[LineController][getTotalRunningTime] e:{}", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
		return new Result(Code.GET_OK, totalRunningTimeList, total);
    }
    
    /**
     * Get the activation of lines.
     * @param null.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the Equipment, or with status {@code 404 (Not Found)}.
     */
    @GetMapping(value = "/getActivation", produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize("hasAuthority('line')")
    public Result getActivation(@RequestParam Map<String, Object> params) {
    	log.debug("REST request to get the activation lines.");
		//List<Line> historyOtputList = null;
    	List<HashMap<Object, String>> activationList = null;
		//long total = 0;
		try {
			if (params == null) {
				return new Result(Code.GET_ERR, "入参为空");
			}
			String beginningTime = null;
			String endTime = null;
			if (params.containsKey("beginningTime")) {
				beginningTime = params.get("beginningTime").toString();
			}
			if (params.containsKey("endTime")) {
				endTime = params.get("endTime").toString();
			}
			activationList = lineService.getActivation(beginningTime, endTime);
			//total = historyOtputList.size();
		} catch (Exception e) {
			log.error("[LineController][getActivation] e:{}", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
		return new Result(Code.GET_OK, activationList, activationList.size());
    }
}

